package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R addAddressBook(@RequestBody AddressBook addressBook) {
        Long userId = UserHolder.getUserId();
        addressBook.setUserId(userId);
        boolean result = addressBookService.save(addressBook);
        return result ? R.success("保存地址成功") : R.error("保存地址失败");
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody AddressBook addressBook) {
        //将该用户所有地址的is_default更新为0
        Long userId = UserHolder.getUserId();
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        lqw.set(AddressBook::getIsDefault, 0);
        addressBookService.update(lqw);

        //将当前设置的默认地址的is_default设置为1
        LambdaUpdateWrapper<AddressBook> lqw1 = new LambdaUpdateWrapper<>();
        lqw1.eq(AddressBook::getUserId, userId);
        lqw1.eq(AddressBook::getId, addressBook.getId());
        lqw1.set(AddressBook::getIsDefault, 1);
        addressBookService.update(lqw1);
        return R.success("设置成功");
    }

    /**
     * 根据Id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getAddressById(@PathVariable Long id) {
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getId, id);
        AddressBook one = addressBookService.getOne(lqw);
        return R.success(one);
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R getDefaultAddress() {
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        lqw.eq(AddressBook::getIsDefault, 1);
        AddressBook one = addressBookService.getOne(lqw);
        return R.success(one);
    }


    /**
     * 查询指定用户的全部地址
     *
     * @return
     */
    @GetMapping("/list")
    public R getAllAddress() {
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids) {
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.in(AddressBook::getId, ids);
        boolean result = addressBookService.remove(lqw);
        return result ? R.success("删除地址成功") : R.error("删除地址失败");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        boolean result = addressBookService.updateById(addressBook);
        return result?R.success("修改成功"):R.error("删除失败");
    }
}
